﻿using System;

namespace KeyWordSearch.ProcessTree
{
	internal sealed class OperatorShortCuttedAndProcess:NonTerminalNode
	{
		public OperatorShortCuttedAndProcess(NonTerminalNode parent)
			: base(parent)
		{
			if (parent == null)
			{
				throw new ArgumentNullException("parent");
			}
		}

		public override ExecuteResult Execute(KeyWordSearch.Normalization.LiveProgramNormalizationResult inspectionTarget)
		{

			if (Childes.Count == 0)
			{
				throw new ProcessInconsistencyException("子ノードが０です");
			}

			ExecuteResult result = new ExecuteResult(true);

			Childes.Reset();

			while (Childes.Next())
			{
				var tmp = Childes.Current.Execute(inspectionTarget);

				result.Merge(tmp, false);

				if (tmp.Answer)
				{
					continue;
				}
				else
				{
					Childes.MoveCurrentToHead();
					break;
				}
			}

			return result;
		}
	}
}
